#!/bin/sh # SDK名字, 改成自己的SDK名字即可

SDK_NAME=XyjsBridge

# framework最后输出的路径的文件夹

UNIVERSAL_OUTPUTFOLDER="${SRCROOT}/Products/"

WORKSPACE_NAME=${PROJECT_NAME}.xcodeproj

# 创建输出路径文件夹

mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"

# 移除上次编译生成的framework

rm -rf "${UNIVERSAL_OUTPUTFOLDER}/${SDK_NAME}.framework"

# 编译真机版framework

xcodebuild -workspace "${WORKSPACE_NAME}" -scheme "${SDK_NAME}" -configuration ${CONFIGURATION} OBJROOT="${OBJROOT}/DependentBuilds" -sdk iphoneos ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" build

# 编译模拟器版framework

xcodebuild -workspace "${WORKSPACE_NAME}" -scheme "${SDK_NAME}" -configuration ${CONFIGURATION} OBJROOT="${OBJROOT}/DependentBuilds" -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" build

# 拷贝编译生成的真机版framework到最终输出的路径

cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${SDK_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}"

# 将模拟器框架的swift模块复制到最终输出的路径

SIMULATOR_SWIFT_MODULES_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${SDK_NAME}.framework/Modules/${SDK_NAME}.swiftmodule/."

if [ -d "${SIMULATOR_SWIFT_MODULES_DIR}" ]; then

cp -R "${SIMULATOR_SWIFT_MODULES_DIR}" "${UNIVERSAL_OUTPUTFOLDER}/${SDK_NAME}.framework/Modules/${SDK_NAME}.swiftmodule"

fi

# 合并模拟器和真机framework, 生成通用framework

lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${SDK_NAME}.framework/${SDK_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${SDK_NAME}.framework/${SDK_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${SDK_NAME}.framework/${SDK_NAME}"

# 删除编译之后生成的无关的配置文件

dir_path="${UNIVERSAL_OUTPUTFOLDER}/${SDK_NAME}.framework/"

for file in ls $dir_path

do

if [[ ${file} =~ ".xcconfig" ]] then

rm -f "${dir_path}/${file}"

fi

done

# 拼接项目名.framework/Headers/项目名-Swift.h 文件名 swift_header="$dir_path/Headers/${SDK_NAME}-Swift.h"

# 在项目名.framework/Headers/项目名-Swift.h里面修改内容

newline1="#if defined(__x86_64__) && __x86_64__ || (__arm64__) && __arm64__ || (__i386__) && __i386__"

#查找#if 0替换成空

sed -i '' 's/#if 0//g' $swift_header

#查找#elif defined(__arm64__) && __arm64__替换成空

sed -i '' 's/#elif defined(__arm64__) && __arm64__//g' $swift_header

#在1第一行添加字符串newline1

sed -i '' "1 a\\ $newline1" $swift_header

# 打开合并后的文件夹 open "${UNIVERSAL_OUTPUTFOLDER}"
